Previous Up Next

Arithmetic and Comparison Operations

\(\bf Binary\ Arithmetic\)

Cool has four binary arithmetic operations: \(\rm +,\ -,\ *,\ /\). The syntax is

expr1 <op> expr2

To evaluate such an expression first \(\rm expr1\) is evaluated and then \(\rm expr2\). The result of the operation is the result of the expression.

The static types of the two sub-expressions must be \(\rm Int\). The static type of the entire arithmetic expression is also \(\rm Int\). Cool has only integer division.

\(\bf Binary\ Relations\)

Cool has three comparison operations: \(\rm <,\ <=,\ =\). These comparisons may be applied to subexpressions of any types, subject to the following rules:

In all cases, the result of the comparison is a \(\rm Bool\). See the type checking rules for more information.

In principle, there is nothing wrong with permitting equality tests between, for example, \(\rm Bool\) and \(\rm Int\). However, such a test must always be false and almost certainly indicates some sort of programming error. The Cool type checking rules catch such errors at compile-time instead of waiting until runtime.

On non-basic objects, equality is decided via pointer equality (i.e., whether the memory addresses of the objects are the same). Equality is defined for \(\rm void\): two \(\rm void\) values are equal and a \(\rm void\) value is never equal to a non-\(\rm void\) value. See the operational semantics rules for more informaiton.

\(\bf Unary\ Expressions\)

Finally, there is one unary arithmetic and one unary logical operator.

Previous Up Next